package com.singleton.type7;

/**
 * @author 薛虎
 * @date 2020/12/16 9:12
 * <p>
 * 静态内部类,线程安全~效率高
 */
public class SingletonTest07 {

    public static void main(String[] args) {
        System.out.println("静态内部类完成单例模式,线程安全~效率高");
        Singleton singleton = Singleton.getInstance();
        Singleton singleton1 = Singleton.getInstance();
        System.out.println(singleton == singleton1); //true
    }

}


//静态内部类,线程安全~效率高
class Singleton {


    private Singleton() {
    }

    //提供一个公有方法,直接返回SingletonInstance.INSTANCE
    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }

    //写一个静态内部类,该类中有一个静态的属性Singleton
    private static class SingletonInstance {
        private static final Singleton INSTANCE = new Singleton();
    }
}
